package com.xbongbong.paas.help;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.esign.exception.DefineException;
import com.xbongbong.esign.helper.AccountHelper;
import com.xbongbong.esign.helper.FileTemplateHelper;
import com.xbongbong.esign.helper.SignHelper;
import com.xbongbong.paas.domain.entity.PaasAppEntity;
import com.xbongbong.paas.domain.entity.ext.PaasFormDataEntityExt;
import com.xbongbong.paas.domain.entity.ext.PaasFormEntityExt;
import com.xbongbong.paas.model.PaasAppModel;
import com.xbongbong.paas.model.PaasFormModel;
import com.xbongbong.paas.pojo.DetailTabPojo;
import com.xbongbong.paas.pojo.WorkFlowPojo;
import com.xbongbong.paas.pojo.vo.UserFormPermissionVO;
import com.xbongbong.paas.redis.PaasRedisHelper;
import com.xbongbong.paas.redis.RedisPrefixConstant;
import com.xbongbong.paas.redis.RedisTimeConstant;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.pojo.PermissionTermsPojo;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.paas.toolbox.wrap.UserVO;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.pro.enums.menu.enums.SystemAppMenuEnum;
import com.xbongbong.pro.enums.permission.enums.ProPermissionAliasEnum;
import com.xbongbong.pro.esign.pojo.EsignDocPojo;
import com.xbongbong.pro.esign.pojo.dto.EsignAuthUrlDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFlowDetailDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignFlowStatusDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignOrderResaleDTO;
import com.xbongbong.pro.esign.pojo.dto.EsignProcessStartDTO;
import com.xbongbong.pro.esign.pojo.dto.XbbFileDetailDTO;
import com.xbongbong.pro.esign.pojo.dto.XbbSignNoticeUrlDTO;
import com.xbongbong.pro.esign.pojo.vo.EsignAuthUrlVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpConsoleVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpInfoVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpRealnameVO;
import com.xbongbong.pro.esign.pojo.vo.EsignCorpStatusVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFileDetailVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFlowDetailVO;
import com.xbongbong.pro.esign.pojo.vo.EsignFlowStatusVO;
import com.xbongbong.pro.esign.pojo.vo.EsignNoticeUrlVO;
import com.xbongbong.pro.esign.pojo.vo.EsignOrderResaleVO;
import com.xbongbong.pro.esign.pojo.vo.EsignProcessStartVO;
import com.xbongbong.pro.formdata.pojo.dto.FormDataDetailDTO;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.domain.entity.EsignSetEntity;
import com.xbongbong.saas.domain.entity.EsignTaskEntity;
import com.xbongbong.saas.enums.BusinessDetailTabEnum;
import com.xbongbong.saas.enums.EsignFlowStatusEnum;
import com.xbongbong.saas.enums.EsignTaskTypeEnum;
import com.xbongbong.saas.enums.WorkFlowEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.model.EsignSetModel;
import com.xbongbong.saas.model.EsignTaskModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;
import java.util.Set;

/**
 * e签宝
 * 创建时间 2020/6/16 4:20 PM
 * 修改时间 2020/6/16 4:20 PM
 * @author chy
 */
@Component
public class EsignHelp {

    @Resource
    public PaasRedisHelper paasRedisHelper;
    @Resource
    private AccountHelper accountHelper;
    @Resource
    private FileTemplateHelper fileTemplateHelper;
    @Resource
    private SignHelper signHelper;
    @Resource
    private EsignSetModel esignSetModel;
    @Resource
    private EsignTaskModel esignTaskModel;
    @Resource
    private PaasAppModel paasAppModel;
    @Resource
    private UserInfoHelp userInfoHelp;
    @Resource
    private PaasFormModel paasFormModel;

    private static final Logger LOG = LoggerFactory.getLogger(EsignHelp.class);

    /**
     * 获取企业e签宝微应用状态
     * @param corpid
     * @return
     * @throws XbbException
     * 创建时间 2020/6/16 4:24 PM
     * 修改时间 2020/6/16 4:24 PM
     * @author chy
     */
    public EsignCorpStatusVO getCorpStatus(String corpid) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.getCorpStatus(corpid);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }

        return json.toJavaObject(EsignCorpStatusVO.class);
    }

    /**
     * 获取授权的页面地址
     * @param esignAuthUrlDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignAuthUrlVO getEsignAuthUrl(EsignAuthUrlDTO esignAuthUrlDTO) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.getAuthUrl(esignAuthUrlDTO.getCorpId(), esignAuthUrlDTO.getRedirectUrl());
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignAuthUrlVO.class);
    }

    /**
     * 套餐转售
     * @param esignOrderResaleDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignOrderResaleVO getEsignOrderResale(EsignOrderResaleDTO esignOrderResaleDTO) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.orderResale(esignOrderResaleDTO.getCorpId(), esignOrderResaleDTO.getOrderId(),
                    esignOrderResaleDTO.getItemCode(), esignOrderResaleDTO.getIteName(),
                    esignOrderResaleDTO.getQuantity(), esignOrderResaleDTO.getOrderCreateTime(),
                    esignOrderResaleDTO.getPayFee());
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignOrderResaleVO.class);
    }


    /**
     * 查询套餐余量
     * @param corpid
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public Integer getEsignContractMargin(String corpid) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.contractMargin(corpid);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.getInteger("margin");
    }


    /**
     * 查询企业信息
     * @param corpid
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignCorpInfoVO getEsignCorpInfo(String corpid) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.corpInfo(corpid);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }

        return json.toJavaObject(EsignCorpInfoVO.class);
    }

    /**
     * 查询个人信息
     * @param corpid
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignCorpInfoVO getEsignUserInfo(String corpid, String userId) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.userInfo(corpid, userId);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignCorpInfoVO.class);
    }


    /**
     * 获取跳转到企业实名的地址
     * @param corpid
     * @param userId
     * @param redirectUrl
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignCorpRealnameVO getEsignCorpRealname(String corpid, String userId, String redirectUrl) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.corpRealname(corpid, userId, redirectUrl);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignCorpRealnameVO.class);
    }


    /**
     * 获取跳转到个人实名的地址
     * @param corpid
     * @param userId
     * @param redirectUrl
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignCorpRealnameVO getEsignUserRealname(String corpid, String userId, String redirectUrl) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.userRealname(corpid, userId, redirectUrl);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignCorpRealnameVO.class);
    }


    /**
     * 获取企业控制台地址
     * @param corpid
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignCorpConsoleVO getEsignCorpConsole(String corpid) throws XbbException {
        JSONObject json = null;
        try {
            json = accountHelper.corpConsole(corpid);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }
        return json.toJavaObject(EsignCorpConsoleVO.class);
    }

    /**
     * 获取发起签署任务地址
     * @param esignProcessStartDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignProcessStartVO getEsignProcessStart(EsignProcessStartDTO esignProcessStartDTO) throws XbbException {
        JSONObject json = null;
        try {
            json = signHelper.processStart(esignProcessStartDTO);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }

        return json.toJavaObject(EsignProcessStartVO.class);
    }

    /**
     * 获取签署人签署地址
     * @param xbbSignNoticeUrlDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignNoticeUrlVO signNoticeUrl(XbbSignNoticeUrlDTO xbbSignNoticeUrlDTO) throws XbbException {

        String corpid = xbbSignNoticeUrlDTO.getCorpid();
        Long formId = xbbSignNoticeUrlDTO.getFormId();
        Long dataId = xbbSignNoticeUrlDTO.getDataId();

        String taskId = getTaskId(corpid, EsignTaskTypeEnum.SIGN.getCode(), formId, dataId);

        JSONObject json = null;
        try {
            json = signHelper.signNoticeUrl(corpid, taskId);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }

        return json.toJavaObject(EsignNoticeUrlVO.class);
    }

    /**
     * 获取对应流程任务详情
     * @param esignFlowDetailDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignFlowDetailVO getEsignFlowDetail(EsignFlowDetailDTO esignFlowDetailDTO) throws XbbException {

        String corpid = esignFlowDetailDTO.getCorpid();
        Long formId = esignFlowDetailDTO.getFormId();
        Long dataId = esignFlowDetailDTO.getDataId();

        String prefix = RedisPrefixConstant.ESIGN_FLOW_DETAIL;
        String key = corpid + "_" + formId + "_" + dataId;
        EsignFlowDetailVO esignFlowDetailVO = JSONObject.parseObject(paasRedisHelper.getValue(prefix,key),EsignFlowDetailVO.class);
        if (!Objects.isNull(esignFlowDetailVO)) {
            return esignFlowDetailVO;
        }

        String taskId = getTaskId(corpid, EsignTaskTypeEnum.SIGN.getCode(),formId, dataId);
        if (StringUtil.isEmpty(taskId)) {
            return new EsignFlowDetailVO();
        }
        JSONObject json = null;
        try {
            json = signHelper.flowDetail(corpid, taskId);
        } catch (DefineException e) {
            return new EsignFlowDetailVO();
        }
        esignFlowDetailVO = json.toJavaObject(EsignFlowDetailVO.class);

        paasRedisHelper.setValue(prefix,key, esignFlowDetailVO, RedisTimeConstant.TINY_DURATION);
        return esignFlowDetailVO;
    }


    /**
     *
     * @param corpid 公司id
     * @param type 任务类型
     * @param formId 表单id
     * @param dataId 数据id
     * @return
     * 创建时间 2020/6/24 6:01 PM
     * 修改时间 2020/6/24 6:01 PM
     * @author chy
     */
    private String getTaskId(String corpid, Integer type, Long formId, Long dataId){
        EsignTaskEntity esignTaskEntity = esignTaskModel.getByFormId(corpid, type, formId, dataId);
        if (Objects.isNull(esignTaskEntity)) {
            return null;
        }
        return esignTaskEntity.getTaskId();
    }

    /**
     * 查询文件详情/下载文件
     * @param xbbFileDetailDTO
     * @return
     * @throws XbbException
     * 创建时间 2020/6/17 1:48 PM
     * 修改时间 2020/6/17 1:48 PM
     * @author chy
     */
    public EsignFileDetailVO getEsignFileDetail(XbbFileDetailDTO xbbFileDetailDTO) throws XbbException {
        String corpid = xbbFileDetailDTO.getCorpid();
        Long formId = xbbFileDetailDTO.getFormId();
        Long dataId = xbbFileDetailDTO.getDataId();
        String taskId = getTaskId(corpid, EsignTaskTypeEnum.SIGN.getCode(), formId, dataId);
        if (StringUtil.isEmpty(taskId)) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, "文件不存在");
        }

        JSONArray flowDocsJson = null;

        try {
            flowDocsJson = fileTemplateHelper.getFlowDocs(corpid, taskId);
        } catch (DefineException e) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100001, e.getMessage());
        }

        List<EsignDocPojo> docs = flowDocsJson.toJavaList(EsignDocPojo.class);

        if (Objects.isNull(docs) || docs.isEmpty()) {
            throw new XbbException(SystemErrorCodeEnum.API_ERROR_100102);
        }

        EsignDocPojo doc = docs.get(0);
        EsignFileDetailVO esignFileDetailVO = new EsignFileDetailVO();
        esignFileDetailVO.setDownloadUrl(doc.getFileUrl());
        return esignFileDetailVO;
    }

    /**
     * 塞入签署动态
     * @param formDataDetailDTO
     * @param paasFormDataEntityExt
     * @param tabPojoList
     * 创建时间 2020/6/24 2:27 PM
     * 修改时间 2020/6/24 2:27 PM
     * @author chy
     */
    public void addEsignTab4Saas(FormDataDetailDTO formDataDetailDTO, PaasFormDataEntityExt paasFormDataEntityExt, List<DetailTabPojo> tabPojoList) throws XbbException {
        UserVO userVO = formDataDetailDTO.getLoginUser();
        String corpid = paasFormDataEntityExt.getCorpid();
        Long formId = paasFormDataEntityExt.getFormId();
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(formDataDetailDTO.getBusinessType());

        UserFormPermissionVO userFormPermissionVO = userInfoHelp.getUserFormPermissionInternal(corpid,
                formDataDetailDTO.getUserId(), paasFormDataEntityExt.getAppId(), paasFormDataEntityExt.getMenuId());

        if (!hasEsignPermission(userFormPermissionVO,userVO.getPermSet(), xbbRefTypeEnum, corpid, formId)) {
            return;
        }

        Long id = paasFormDataEntityExt.getId();
        //获取签署状态
        EsignFlowStatusDTO esignFlowStatusDTO = new EsignFlowStatusDTO();
        BeanUtil.copyProperties(formDataDetailDTO, esignFlowStatusDTO);
        esignFlowStatusDTO.setCorpid(formDataDetailDTO.getCorpid());
        esignFlowStatusDTO.setFormId(formId);
        esignFlowStatusDTO.setDataId(id);
        EsignFlowStatusVO esignFlowStatusVO = getEsignFlowStatus(esignFlowStatusDTO);
        EsignFlowStatusEnum esignFlowStatusEnum = EsignFlowStatusEnum.getByCode(esignFlowStatusVO.getCode());
        Boolean effectiveFlowStatus = false;
        if (Objects.equals(esignFlowStatusEnum, EsignFlowStatusEnum.SIGNING) ||
                Objects.equals(esignFlowStatusEnum, EsignFlowStatusEnum.COMPLETE)) {
            effectiveFlowStatus = true;
        }
        /**
         * 流程状态(0-草稿,1-签署中,2-完成,3-撤销,4-终止（签署流程设置了文件有效截至日期，到期后触发),5-过期（签署截至日志到期后触发),7-拒签)
         */


        List<BusinessDetailTabEnum> tabEnumList = BusinessDetailTabEnum.getByBusinessType(XbbRefTypeEnum.ESIGN.getCode());
        DetailTabPojo detailTabPojo = new DetailTabPojo();
        for (BusinessDetailTabEnum tabEnum : tabEnumList) {
            BeanUtil.copyProperties(tabEnum.getAnEnum(), detailTabPojo);
            detailTabPojo.setAttr(detailTabPojo.getKey());
        }
        if (effectiveFlowStatus) {
            tabPojoList.add(detailTabPojo);
        }
    }

    /**
     * 判断是否有e签宝相关权限
     * 1、开启e签宝应用
     * 2、对应业务，有对应的e签宝签署权限
     * 3、对应的表单开启了e签宝签署
     * @param userFormPermissionVO
     * @param permSet
     * @param xbbRefTypeEnum
     * @param corpid
     * @param formId
     * @return
     */
    public Boolean hasEsignPermission(UserFormPermissionVO userFormPermissionVO, Set<String> permSet, XbbRefTypeEnum xbbRefTypeEnum, String corpid, Long formId) {
        if (Objects.isNull(userFormPermissionVO)) {
            return hasEsignPermission(null, null, permSet, xbbRefTypeEnum, corpid, formId);
        } else {
            return hasEsignPermission(userFormPermissionVO.getFormPermission().getPermission(), userFormPermissionVO.getIsAdminOrBoss(), permSet, xbbRefTypeEnum, corpid, formId);
        }
    }

    public Boolean hasEsignPermission(PermissionTermsPojo permissionTerms, Boolean isAdminOrBoss, Set<String> permSet, XbbRefTypeEnum xbbRefTypeEnum, String corpid, Long formId) {
        PaasAppEntity paasApp = paasAppModel.getByAlias(SystemAppMenuEnum.ESIGN.getAlias(), corpid);
        if (Objects.isNull(paasApp) || Objects.equals(paasApp.getEnable(), BasicConstant.ZERO)) {
            return false;
        }
        if (Objects.isNull(xbbRefTypeEnum)) {
            return false;
        }

        xbbRefTypeEnum = Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.SYSTEM) ? XbbRefTypeEnum.PAAS : xbbRefTypeEnum;

        Boolean hasContractEsign = Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.CONTRACT) &&
                permSet.contains(ProPermissionAliasEnum.CONTRACT_ESIGN.getAlias());
        Boolean hasPurchaseEsign = Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.PURCHASE) &&
                permSet.contains(ProPermissionAliasEnum.PURCHASE_ESIGN.getAlias());
        Boolean hasQuotationEsign = Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.QUOTATION) &&
                permSet.contains(ProPermissionAliasEnum.QUOTATION_ESIGN.getAlias());
        Boolean hasPaasEsign = false;
        if (Objects.equals(xbbRefTypeEnum, XbbRefTypeEnum.PAAS)) {
            if ( permissionTerms.getEsign().equals(1) || isAdminOrBoss ) {
                hasPaasEsign = true;
            }
        }
        if (!hasContractEsign && !hasPurchaseEsign && !hasQuotationEsign && !hasPaasEsign) {
            return false;
        }

        EsignSetEntity esignSet = esignSetModel.getByFormId(corpid, formId);
        if (Objects.isNull(esignSet) || !Objects.equals(esignSet.getEnable(), BasicConstant.ONE)) {
            return false;
        }
        return true;
    }



    /**
     * 添加e签宝按钮
     * @param workFlowPojoList
     * @param workFlowEnum
     * @param xbbRefTypeEnum
     * @param paasFormDataEntityExt
     * 创建时间 2020/6/29 4:18 PM
     * 修改时间 2020/6/29 4:18 PM
     * @author chy
     */
    public Boolean addEsignWorkFlow(Set<String> permSet, List<WorkFlowPojo> workFlowPojoList, WorkFlowEnum workFlowEnum, XbbRefTypeEnum xbbRefTypeEnum, PaasFormDataEntityExt paasFormDataEntityExt) {

        Boolean isEsignWorkFlow = false;

        Boolean isContractEsignBtn = Objects.equals(workFlowEnum, WorkFlowEnum.CONTRACT_ESIGN) ||
                Objects.equals(workFlowEnum, WorkFlowEnum.CONTRACT_ESIGN_DOWNLOAD);
        Boolean isPurchaseEsignBtn = Objects.equals(workFlowEnum, WorkFlowEnum.PURCHASE_ESIGN) ||
                Objects.equals(workFlowEnum, WorkFlowEnum.PURCHASE_ESIGN_DOWNLOAD);
        Boolean isQuotationEsignBtn = Objects.equals(workFlowEnum, WorkFlowEnum.QUOTATION_ESIGN) ||
                Objects.equals(workFlowEnum, WorkFlowEnum.QUOTATION_ESIGN_DOWNLOAD);

        if (isContractEsignBtn || isPurchaseEsignBtn || isQuotationEsignBtn) {
            isEsignWorkFlow = true;
        }

        String corpid = paasFormDataEntityExt.getCorpid();
        //这边为非自定义表单操作
        if (!hasEsignPermission(null,permSet, xbbRefTypeEnum, corpid, paasFormDataEntityExt.getFormId())){
            return isEsignWorkFlow;
        }

        if (!isContractEsignBtn && !isPurchaseEsignBtn && !isQuotationEsignBtn) {
            return isEsignWorkFlow;
        }
        Long formId = paasFormDataEntityExt.getFormId();
        Long id = paasFormDataEntityExt.getId();

        EsignFlowStatusDTO esignFlowStatusDTO = new EsignFlowStatusDTO();
        esignFlowStatusDTO.setCorpid(corpid);
        esignFlowStatusDTO.setFormId(formId);
        esignFlowStatusDTO.setDataId(id);
        EsignFlowDetailVO esignFlowDetailVO = null;
        try {
            EsignFlowDetailDTO esignFlowDetailDTO = new EsignFlowDetailDTO();
            esignFlowDetailDTO.setCorpid(paasFormDataEntityExt.getCorpid());
            esignFlowDetailDTO.setFormId(paasFormDataEntityExt.getFormId());
            esignFlowDetailDTO.setDataId(paasFormDataEntityExt.getDataId());
            esignFlowDetailVO = getEsignFlowDetail(esignFlowDetailDTO);
        } catch (XbbException e) {
            return isEsignWorkFlow;
        }
        Integer code = esignFlowDetailVO.getFlowStatus();
        EsignFlowStatusEnum esignFlowStatusEnum = EsignFlowStatusEnum.getByCode(code);
        switch (esignFlowStatusEnum) {
            case COMPLETE:
                if (Objects.equals(workFlowEnum, WorkFlowEnum.CONTRACT_ESIGN_DOWNLOAD) ||
                        Objects.equals(workFlowEnum, WorkFlowEnum.PURCHASE_ESIGN_DOWNLOAD) ||
                        Objects.equals(workFlowEnum, WorkFlowEnum.QUOTATION_ESIGN_DOWNLOAD)) {
                    WorkFlowPojo workFlowPojo = new WorkFlowPojo();
                    BeanUtil.copyProperties(workFlowEnum, workFlowPojo);
                    workFlowPojoList.add(workFlowPojo);
                }
                break;
            case SIGNING:
                //签订中无法发起签署、无法下载
                break;
            case UNSIGNED:
            case DRAFT:
            case UNDO:
            case TERMINATION:
            case EXPIRED:
            case REFUSAL:
                if (Objects.equals(workFlowEnum, WorkFlowEnum.CONTRACT_ESIGN) ||
                        Objects.equals(workFlowEnum, WorkFlowEnum.PURCHASE_ESIGN) ||
                        Objects.equals(workFlowEnum, WorkFlowEnum.QUOTATION_ESIGN)) {
                    WorkFlowPojo workFlowPojo = new WorkFlowPojo();
                    BeanUtil.copyProperties(workFlowEnum, workFlowPojo);
                    workFlowPojoList.add(workFlowPojo);
                }
                break;
            default:
                break;
        }
        return isEsignWorkFlow;
    }

    /**
     * 获取签署状态
     * @param esignFlowStatusDTO
     * @return
     * @throws XbbException
     */
    public EsignFlowStatusVO getEsignFlowStatus(EsignFlowStatusDTO esignFlowStatusDTO) throws XbbException {

        Long formId = esignFlowStatusDTO.getFormId();
        String corpid = esignFlowStatusDTO.getCorpid();
        PaasFormEntityExt paasForm = paasFormModel.getByKey(formId, corpid);

        if (Objects.isNull(paasForm)) {
            EsignFlowStatusVO esignFlowStatusVO = new EsignFlowStatusVO();
            return esignFlowStatusVO;
        }

        Integer businessType = paasForm.getBusinessType();
        XbbRefTypeEnum xbbRefTypeEnum = XbbRefTypeEnum.getByCode(businessType);

        UserFormPermissionVO userFormPermissionVO = userInfoHelp.getUserFormPermissionInternal(corpid,
                esignFlowStatusDTO.getUserId(), paasForm.getAppId(), paasForm.getMenuId());

        if (!hasEsignPermission(userFormPermissionVO,esignFlowStatusDTO.getLoginUser().getPermSet(), xbbRefTypeEnum, corpid, formId)) {
            EsignFlowStatusVO esignFlowStatusVO = new EsignFlowStatusVO();
            return esignFlowStatusVO;
        }

        EsignFlowDetailDTO esignFlowDetailDTO = new EsignFlowDetailDTO();
        BeanUtil.copyProperties(esignFlowStatusDTO, esignFlowDetailDTO);

        EsignFlowDetailVO esignFlowDetailVO = getEsignFlowDetail(esignFlowDetailDTO);
        Integer flowStatus = esignFlowDetailVO.getFlowStatus();

        EsignFlowStatusEnum esignFlowStatusEnum = EsignFlowStatusEnum.getByCode(flowStatus);
        EsignFlowStatusVO esignFlowStatusVO = new EsignFlowStatusVO();
        esignFlowStatusVO.setCode(esignFlowStatusEnum.getCode());
        esignFlowStatusVO.setName(esignFlowStatusEnum.getName());
        return esignFlowStatusVO;
    }
}
